const path = require('path'); 
const fs = require('fs'); 
module.exports = function(opt, glyphList) {

  let cssFolder = path.dirname(opt.cssPath)
  let eotSrc = path.relative(cssFolder, opt.eotPath);
  let woffSrc = path.relative(cssFolder, opt.woffPath);
  let ttfSrc = path.relative(cssFolder, opt.ttfPath);
  let svgSrc = path.relative(cssFolder, opt.svgPath);

  if (typeof opt.publicPath === 'string') {
    eotSrc = path.join(opt.publicPath, `${opt.fontName}.eot`);
    woffSrc = path.join(opt.publicPath, `${opt.fontName}.woff`);
    ttfSrc = path.join(opt.publicPath, `${opt.fontName}.ttf`);
    svgSrc = path.join(opt.publicPath, `${opt.fontName}.svg`);
  }

  let srcStr = [
    eotSrc ? `url(${eotSrc}?#iefix) format("eot")` : '',
    woffSrc ? `url(${woffSrc}) format("woff")` : '',
    ttfSrc ? `url(${ttfSrc}) format("truetype")` : '',
    svgSrc ? `url(${svgSrc}#${opt.fontName}) format("svg")` : '',
  ].filter(v => v !== '').join(',');

  let cssText = glyphList.map(function (v) {
    return `.${opt.classPrefix + v.name}:before { content: "\\${v.code.toString(16)}"; }`;
  }).join('\n');

  return `/*svgfont---start*/
@font-face {
  font-family: "${opt.fontName}";${eotSrc ? `\n  src: url(${eotSrc}); /* IE9 */` : ''}
  src: ${srcStr};
}
.${opt.baseClass} {
  font-family: "${opt.fontName}" !important;
  font-size:16px;
  font-style:normal;
  line-height: 1;
  vertical-align: middle;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
${cssText}
/*svgfont---end*/`;
};

